package nps.pay.servlet;

import nps.pay.config.NpsConfig;
import nps.pay.utils.EncryptUtil;
import nps.pay.utils.HttpUtils;
import nps.pay.utils.SignUtils;
import okhttp3.Response;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

public class PayServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderNo = req.getParameter("orderNo");
        //String orderNo = System.currentTimeMillis() + "D";
        String money = req.getParameter("money");
        String bankCode = req.getParameter("bankCode");
        String cardNo = "";
        String busCode = "1021";//3605 1021网银

        Map<String, String> dataMap = fullRequestData(orderNo, money, bankCode, cardNo, busCode);
        Response response = HttpUtils.sendRequest(NpsConfig.PAY_URL, dataMap);
        String result = response.body().string();
        System.out.println("响应结果：" + result);
        Document reqDoc = null;
        try {
            reqDoc = DocumentHelper.parseText(result);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element rootEl = reqDoc.getRootElement();
        String RESP_CODE = rootEl.elementText("RESP_CODE");
        String PAY_INFO = rootEl.elementText("PAY_INFO");
        String RESP_DESC = rootEl.elementText("RESP_DESC");
        if ("0000".equals(RESP_CODE)) {
            String decode_str = EncryptUtil.getFromBase64(PAY_INFO);
            System.out.println("decode_str=" + decode_str);
            req.setAttribute("html", decode_str);
            req.getRequestDispatcher("pay.jsp").forward(req, resp);
        } else {
            req.setAttribute("errorMsg", RESP_DESC);
            req.getRequestDispatcher("error.jsp").forward(req, resp);
        }

        //http://localhost/payServlet?orderNo=0123&money=1.00&cardNo=6227000062751278199&bankCode=01020000
        //http://dream-zhibi.tunnel.qydev.com/payServlet?orderNo=123&money=4.00&bankCode=03050000
    }

    /**
     * 填充请求参数
     *
     * @param orderNo
     * @param money
     * @param bankCode
     * @param cardNo
     * @param busCode
     * @return
     */
    private Map<String, String> fullRequestData(String orderNo, String money, String bankCode, String cardNo, String busCode) {
        Map<String, String> dataMap = new LinkedHashMap<>();
        dataMap.put("ORDER_ID", orderNo);//订单号
        dataMap.put("ORDER_AMT", money);//元
        dataMap.put("ORDER_TIME", new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));//YYYYMMDDHHMMSS
        dataMap.put("USER_TYPE", "02");//
        dataMap.put("USER_ID", NpsConfig.USER_ID);//
        dataMap.put("SIGN_TYPE", "03");//
        dataMap.put("BUS_CODE", busCode);//
        dataMap.put("CCT", "CNY");//
        dataMap.put("PAY_TYPE", "01");//
        dataMap.put("CARD_TYPE", "01");//
        dataMap.put("BANK_CODE", bankCode);//
        dataMap.put("CARD_NO", cardNo);//银行卡号
        dataMap.put("SIGN", SignUtils.sign(dataMap));
        //上面的需要参与签名
        dataMap.put("GOODS_NAME", "测试支付");//商户名称
        dataMap.put("GOODS_DESC", "测试支付");//
        dataMap.put("BG_URL", "http://dream-zhibi.tunnel.qydev.com/notifyServlet");//后台通知地址
        dataMap.put("PAGE_URL", "http://dream-zhibi.tunnel.qydev.com/notifyServlet");//支付成功界面
        return dataMap;
    }
}
